package com.nacrtstudio.webgiftshop.pages.admin;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.EventLink;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.nacrtstudio.webgiftshop.facade.Controller;
import com.nacrtstudio.webgiftshop.model.shop.Category;
import com.nacrtstudio.webgiftshop.model.shop.Gift;

@RequiresRoles("admin")
public class Catalog {
	@Inject
	private Controller controller;
	@Property
	private Gift gift;
	@InjectPage
	private EditGift editGift;

	public List<Gift> getAllGifts() {
		return controller.getAllGifts();
	}
	
	public Object onEdit(Long id) {
		editGift.setEditGift(controller.findGift(id));
		return editGift;
	}

	public void onDelete(Long id) {
		try {
			controller.deleteGift(controller.findGift(id));
		} catch (Exception e) {
			System.out.println("**********************Error on delete********************\n"+e);
		}
	}
	
	@Component(parameters={"event=changeStatus","context=gift.giftId"})
	private EventLink changeStatusToInactive, changeStatusToActive;
	
	public void onChangeStatus(Long id){
		try {
			Gift gift = controller.findGift(id);
			if (gift.isStatus()) {
				gift.setStatus(false);
				controller.updateGift(gift);
			} else {
				gift.setStatus(true);
				controller.updateGift(gift);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
